<?php
namespace App\Admin\Controllers\Operation;

use App\Models\Garage\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

use App\Models\Operation\KeyDelivery as ThisModel;

class KeyDeliveryController extends AdminController
{
    protected $title = '寄钥匙';

    protected function grid(): Grid
    {
        $pay_types = ThisModel::getPayTypes();
        $users = User::getUsers();

        $grid = new Grid(new ThisModel);

        $grid->model()->orderBy('id', 'desc');

        $grid->column('id', __('ID'))->sortable();
        $grid->column('client_name', __('Client name')); // 三方 ID
        $grid->column('plate_number', __('Plate number'));
        $grid->column('tracking_number', __('Tracking number'));
        $grid->column('explanation', __('Explanation'));
        $grid->column('address', __('Address'));
        $grid->column('remark', __('Remark'));
        $grid->column('item_sendback', __('Item sendback'))->using($pay_types);
        $grid->column('pay_type', __('Pay type'))->using($pay_types);
        $grid->column('created_by', __('Created by'))->display(function ($val) use ($users) {
            return sprintf('<a href="/admin/garage/users?id=%s" target="_blank">%s</a>', $val, $users[$val] ?: __("Unknown"));
        });
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->column(1/3, function ($filter) {
                $filter->equal('id', __('ID'));
                $filter->equal('name', __('Name'));
                $filter->like('plate_number', __('Plate number'));
            });
            $filter->column(1/3, function ($filter) {
                $filter->like('address', __('Address'));
                $filter->like('remark', __('Remark'));
                $filter->like('explanation', __('Explanation'));
            });
            $filter->column(1/3, function ($filter) {
                $filter->between('created_at', __('Created at'))->date();
                $filter->between('updated_at', __('Updated at'))->date();
                $filter->like('tracking_number', __('Tracking number'));
            });
        });

        $grid->disableCreateButton();
        $grid->disableBatchActions();
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
            $actions->disableView();
        });

        return $grid;
    }
    protected function detail($id): Show
    {
        $show = new Show(ThisModel::findOrFail($id));

        $show->field('id', __('ID'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }
    protected function form(): Form
    {
        $pay_types = ThisModel::getPayTypes();
        $users = User::getUsers();

        $form = new Form(new ThisModel);

        // $form->display('id', __('ID'));
        $form->display('client_name', __('Client name')); // 三方 ID
        $form->display('plate_number', __('Plate number'));
        $form->display('tracking_number', __('Tracking number'));
        $form->display('explanation', __('Explanation'));
        $form->display('address', __('Address'));
        $form->display('remark', __('Remark'));
        $form->select('item_sendback', __('Item sendback'))->options($pay_types)->readOnly();
        $form->select('pay_type', __('Pay type'))->options($pay_types)->readOnly();
        $form->display('created_by', __('Created by'))->with(function ($value) use ($users) {
            return $users[$value] ?? '未知';
        });
        // $form->display('created_at', __('Created At'));
        // $form->display('updated_at', __('Updated At'));
        $form->model()->form($form);

        $form->disableViewCheck()
            ->disableCreatingCheck();

        $form->tools(function (Form\Tools $tools) {
            $tools->disableDelete();
            $tools->disableView();
        });

        return $form;
    }
}
